package net.telesing.tcmc.back.action;

import net.telesing.tcmc.back.logic.SectionLogic;
import net.telesing.tcmc.back.model.SectionModel;
import net.telesing.tcmc.back.model.UserModel;
import net.telesing.tcmc.back.modelqp.SectionQp;
import net.telesing.tcmc.common.Constants;
import net.telesing.tcmc.common.LogicException;
import net.telesing.tcmc.common.QueryReturn;
import net.telesing.tcmc.core.web.Ajax;
import net.telesing.tcmc.core.web.AjaxBean;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 号段管理
 * Created by bys on 2015/11/3.
 */
@At("/sect")
@IocBean
public class SectionAcion
{
    private static final Logger log = LogManager.getLogger(SectionAcion.class);
    @Inject
    private SectionLogic sectionLogic;

    /**
     * 跳转到号段管理管理页面
     */
    @At
    @Ok("jsp:${obj.jsp}")
    public Map<String, Object> toList(@Param("flg") int flg,@Param("..") SectionQp qp, @Attr(Constants.SESSION_KEY) UserModel op)
    {
        log.info("sect>>>>>>toList");
        Map<String, Object> map = new HashMap<String, Object>();
        if (qp.getPage() == 0)
        {
            qp.setPage(1);
        }
        qp.setRows(20);
        map.put("qReturn", sectionLogic.doList(qp, op));
        map.put("jsp", sectionLogic.getJsp(flg));
        map.put("sectPage", qp.getPage());
        map.put("isAdmin", sectionLogic.checkUser(op));
        return map;
    }

    @At
    //    @POST
    @Ok("jsp:pages.back.section.new_modify_sect")
    public Map<String, Object> findId(@Param("id") long id, @Attr(Constants.SESSION_KEY) UserModel op)
    {
        log.info("sect>>>>>>findId");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("sectionModel", sectionLogic.find(id));
        return map;
    }

    /**
     * 根据条件查询
     *
     * @param qp
     * @return
     */
    @At
    @POST
    @Ok("json")
    public QueryReturn doList(@Param("..") SectionQp qp, @Attr(Constants.SESSION_KEY) UserModel op)
    {
        log.info("sect>>>>>>doList");
        return sectionLogic.doList(qp, op);
    }

    /**
     * 号段管理新增
     *
     * @param sectionModel
     * @param op
     * @return
     */
    @At
    @POST
    @Ok("json")
    public AjaxBean create(@Param("..") SectionModel sectionModel, @Attr(Constants.SESSION_KEY) UserModel op)
    {
        log.info("sect>>>>>>create");
        try
        {
            return Ajax.ok().setData(sectionLogic.create(sectionModel, op.getId()));
        }
        catch (LogicException e)
        {
            return Ajax.fail().setMsg(e.getMessage());
        }
    }

    /**
     * 号段管理修改
     *
     * @param sectionModel
     * @param op
     * @return
     */
    @At
    @POST
    @Ok("json")
    public AjaxBean modify(@Param("..") SectionModel sectionModel, @Attr(Constants.SESSION_KEY) UserModel op)
    {
        log.info("sect>>>>>>modify");
        try
        {
            return Ajax.ok().setData(sectionLogic.modify(sectionModel, 1));
        }
        catch (LogicException e)
        {
            return Ajax.fail().setMsg(e.getMessage());
        }
    }

    /**
     * 号段状态操作
     * @param id
     * @param state
     * @param op
     * @return
     * @throws LogicException
     */
    @At
    @POST
    @Ok("json")
    public AjaxBean modifyState(@Param("id") long id,@Param("state") int state,@Attr(Constants.SESSION_KEY) UserModel op) throws LogicException
    {
        log.info("sect>>>>>>delete");
            try
            {
                sectionLogic.modifyState(id,op.getId(),state);
                return Ajax.ok();
            }
            catch (LogicException e)
            {
                return Ajax.fail().setMsg(e.getMessage());
            }
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     * @throws LogicException
     */
    @At
    @POST
    @Ok("json")
    public AjaxBean delete(@Param("ids") long[] ids) throws LogicException
    {
        log.info("sect>>>>>>delete");
        if (ids.length != 0)
        {
            try
            {
                sectionLogic.delete(ids);
                return Ajax.ok();
            }
            catch (LogicException e)
            {
                return Ajax.fail().setMsg(e.getMessage());
            }
        }
        else
        {
            return Ajax.fail().setMsg("请选择删除项！");
        }
    }
}
